package com.xw.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.tea.TeaException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.xw.common.Constants;
import com.xw.common.Result;
import com.xw.exp.ServiceException;
import com.xw.manger.AliyunManger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static com.xw.manger.AliyunManger.*;


/**
 * @Description: 阿里云视频点播相关接口
 * @Author: 72478
 * @Date: 2024/12/18 16:53
 * @Version: 1.0
 */
@RestController
@RequestMapping("/aliyun")
@Api(tags = "阿里云视频点播相关接口")
public class AliyunController {

    @Resource
    private AliyunManger aliyunManger;


    @PostMapping("upload")
    @ApiOperation(value = "云点播文件上传")
    public Result<?> upload(@RequestParam MultipartFile file) throws IOException {
        try {
            DefaultAcsClient defaultAcsClient = aliyunManger.initVodClient();
            // 1 获取文件名
            String originalFilename = file.getOriginalFilename();
            // 获取上传凭证
            CreateUploadVideoResponse createUploadVideoResponse = aliyunManger.createUploadVideo(originalFilename, originalFilename, defaultAcsClient);
            // 执行成功会返回VideoId、UploadAddress和UploadAuth
            String videoId = createUploadVideoResponse.getVideoId();
            //解析拿到结果对象
            JSONObject uploadAuth = JSONObject.parseObject(aliyunManger.decodeBase64(createUploadVideoResponse.getUploadAuth()));
            JSONObject uploadAddress = JSONObject.parseObject(aliyunManger.decodeBase64(createUploadVideoResponse.getUploadAddress()));
            // 转换成File对象
            File localFile = aliyunManger.convertMultipartFileToFile(file);
            // 使用UploadAuth和UploadAddress初始化OSS客户端
            OSSClient ossClient = initOssClient(uploadAuth, uploadAddress);
            // 上传文件，注意是同步上传会阻塞等待，耗时与文件大小和网络上行带宽有关
            uploadLocalFile(ossClient, uploadAddress, localFile);
            return new Result<>(videoId, Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(null, Constants.ERROR);
        }
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("removeAlyVideo/{id}")
    public Result<?> removeAlyVideo(@PathVariable String id) {
        aliyunManger.deleteVideo(id);
       return new Result<>(Constants.SUCCESS);
    }

    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @DeleteMapping("delete-batch")
    public Result<?> deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        try {
            //初始化对象
            DefaultAcsClient client = aliyunManger.initVodClient();
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList值转换成 1,2,3
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");

            //向request设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("20001", "删除视频失败");
        }
        return new Result<>(Constants.SUCCESS);
    }


    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public Result<?> getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    aliyunManger.initVodClient();
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            HashMap<String, Object> map = new HashMap<>();
            map.put("playAuth", playAuth);
            return new Result<>(map, Constants.SUCCESS);
        } catch (Exception e) {
            throw new ServiceException("20001", "获取播放凭证失败");
        }
    }

}
